package com.ibeeking.found.workflow.b.rest.controller.v1;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.page.PageMethod;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.workflow.b.rest.service.FlowMessageService;
import com.ibeeking.found.workflow.b.rest.utils.MyPageUtil;
import com.ibeeking.found.workflow.service.common.annotation.MyRequestBody;
import com.ibeeking.found.workflow.service.common.constants.FlowMessageType;
import com.ibeeking.found.workflow.service.common.constants.enums.ErrorCodeEnum;
import com.ibeeking.found.workflow.service.common.dos.FlowMessage;
import com.ibeeking.found.workflow.service.common.param.MyPageData;
import com.ibeeking.found.workflow.service.common.param.MyPageParam;
import com.ibeeking.found.workflow.service.common.vo.FlowMessageVo;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作流消息操作控制器类。
 *
 * @author Jerry
 * @date 2021-06-06
 */
@Slf4j
@RestController
@RequestMapping("b/flow/flowMessage/v1")
public class FlowMessageController {

    @Resource
    private FlowMessageService flowMessageService;

    /**
     * 获取当前用户的未读消息总数。
     * NOTE：白名单接口。
     *
     * @return 应答结果对象，包含当前用户的未读消息总数。
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前用户的未读消息总数")
    @GetMapping(value = "/getMessageCount")
    public ResponseResult<JSONObject> getMessageCount(@LoginUser(isFull = true) CurrentUser currentUser) {
        JSONObject resultData = new JSONObject();
        resultData.put("remindingMessageCount", flowMessageService.countRemindingMessageListByUser(currentUser));
        resultData.put("copyMessageCount", flowMessageService.countCopyMessageByUser(currentUser));
        return ResponseResult.success(resultData);
    }

    /**
     * 获取当前用户的催办消息列表。
     * 不仅仅包含，其中包括当前用户所属角色、部门和岗位的候选组催办消息。
     * NOTE：白名单接口。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前用户的催办消息列表")
    @PostMapping(value = "/listRemindingTask")
    public ResponseResult<MyPageData<FlowMessageVo>> listRemindingTask(@LoginUser(isFull = true) CurrentUser currentUser, @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<FlowMessage> flowMessageList = flowMessageService.getRemindingMessageListByUser(currentUser);
        return ResponseResult.success(MyPageUtil.makeResponseData(flowMessageList, FlowMessage.INSTANCE));
    }

    /**
     * 获取当前用户的抄送消息列表。
     * 不仅仅包含，其中包括当前用户所属角色、部门和岗位的候选组抄送消息。
     * NOTE：白名单接口。
     *
     * @param read true表示已读，false表示未读。
     * @return 应答结果对象，包含查询结果集。
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前用户的抄送消息列表")
    @PostMapping(value = "/listCopyMessage")
    public ResponseResult<MyPageData<FlowMessageVo>> listCopyMessage(@LoginUser(isFull = true) CurrentUser currentUser,
            @MyRequestBody MyPageParam pageParam, @MyRequestBody Boolean read) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<FlowMessage> flowMessageList = flowMessageService.getCopyMessageListByUser(currentUser, read);
        return ResponseResult.success(MyPageUtil.makeResponseData(flowMessageList, FlowMessage.INSTANCE));
    }

    /**
     * 读取抄送消息，同时更新当前用户对指定抄送消息的读取状态。
     *
     * @param messageId 消息Id。
     * @return 应答结果对象。
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "读取抄送消息")
    @PostMapping(value = "/readCopyTask")
    public ResponseResult<Void> readCopyTask(@LoginUser(isFull = true) CurrentUser currentUser, @MyRequestBody Long messageId) {
        String errorMessage;
        // 验证流程任务的合法性。
        FlowMessage flowMessage = flowMessageService.getById(messageId);
        if (flowMessage == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST.getErrorCode(), ErrorCodeEnum.DATA_NOT_EXIST.getErrorMessage());
        }
        if (flowMessage.getMessageType() != FlowMessageType.COPY_TYPE) {
            errorMessage = "数据验证失败，当前消息不是抄送类型消息！";
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED.getErrorCode(), errorMessage);
        }
        if (!flowMessageService.isCandidateIdentityOnMessage(currentUser, messageId)) {
            errorMessage = "数据验证失败，当前用户没有权限访问该消息！";
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED.getErrorCode(), errorMessage);
        }
        flowMessageService.readCopyTask(currentUser, messageId);
        return ResponseResult.success();
    }
}
